package com.hlxt.nj.busi.action;

import com.alibaba.fastjson.JSON;
import com.hlxt.nj.busi.model.TCourse;
import com.hlxt.nj.busi.model.TCourseCriteria;
import com.hlxt.nj.busi.model.TUserCou;
import com.hlxt.nj.busi.model.TUserCouCriteria;
import com.hlxt.nj.busi.service.CourseService;
import com.hlxt.nj.busi.service.UserCouService;
import com.hlxt.nj.common.*;
import com.hlxt.nj.sys.action.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.*;


@Controller
@RequestMapping("/course")
public class CourseAction extends BaseAction {


    @Autowired
    private CourseService courseService;
    @Autowired
    private UserCouService userCouService;



    /**
     * 查询样本(分页)
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryCourse.do")
    public ModelAndView queryCourse(String cname, String maxpop, String isgrade ) throws UnsupportedEncodingException {
        PageBean pageBean = getPageBean();
        TCourseCriteria courseCriteria = new TCourseCriteria();
        TCourseCriteria.Criteria criteria = courseCriteria.createCriteria();
        if(!StringUtil.isEmpty(cname)){
            criteria.andCnameLike(cname);
        }
         if(!StringUtil.isEmpty(maxpop)){
            criteria.andMaxpopEqualTo(Integer.valueOf(maxpop));
        }
         if(!StringUtil.isEmpty(isgrade)){
            criteria.andIsgradeEqualTo(isgrade);
        }
        courseService.selectPageBeanByExample(courseCriteria,pageBean);
        /*时间格式转化*/
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        List<TCourse> list = pageBean.getResults();
        for (TCourse tCourse : list) {
            Map<String,Object> courseMap = new LinkedHashMap<String,Object>();
            courseMap.put("id", tCourse.getId());
            courseMap.put("cname", tCourse.getCname());
            courseMap.put("idgrade",tCourse.getIsgrade());
            courseMap.put("maxpop",tCourse.getMaxpop());
            courseMap.put("count",tCourse.getCount());
            resultList.add(courseMap);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", resultList);
        map.put("total", pageBean.getTotalRecords());
        String json = JSON.toJSONString(map);
        return new ModelAndView("jsonView", "ajax_json", json);
    }

    /**
     * 修改基本信息
     * @param id
     * @param cname
     * @return
     */
    @RequestMapping("/update.do")
    @ResponseBody
    public RespMessage update(String id, String cname,String isgrade,String maxpop) {
        if (!StringUtil.isEmpty(cname,id,isgrade,maxpop)&&cname.length()<=50) {
            TCourse tCourse = new TCourse();
            tCourse.setCname(cname);
            tCourse.setId(id);
            tCourse.setIsgrade(isgrade);
            tCourse.setMaxpop(Integer.valueOf(maxpop));
            boolean flag =courseService.updateByPrimaryKey(tCourse);
            if (flag) {
                log.info("修改课程基本信息成功,id="+id+",name="+cname);
                return RespMessageUtil.getResp(RetCodeDef.SUCCESS, "");
            } else {
                log.info("修改课程信息失败,SQL异常,id="+id+",name="+cname);
                return RespMessageUtil.getResp(RetCodeDef.SQL_ERROR, "");
            }
        } else {
            log.info("修改课程信息失败,参数鉴权失败");
            return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
        }
    }

    @RequestMapping("/insert.do")
    @ResponseBody
    public RespMessage insert( String cname,String isgrade,String maxpop) {

        if (!StringUtil.isEmpty(cname,isgrade,maxpop)&&cname.length()<=50) {
            TCourse tCourse = new TCourse();
            String id = TimeUtil.date8().substring(2, 8)+SequenceUtil.formatSequence(SequenceUtil.getInstance()
                    .getSequence4File("t_course"), 10);
            tCourse.setId(id);
            tCourse.setCname(cname);
            tCourse.setIsgrade(isgrade);
            tCourse.setMaxpop(Integer.valueOf(maxpop));
            boolean flag =courseService.insert(tCourse);
            if (flag) {
                log.info("添加课程基本信息成功,id="+id+",name="+cname);
                return RespMessageUtil.getResp(RetCodeDef.SUCCESS, "");
            } else {
                log.info("添加课程信息失败,SQL异常,id="+id+",name="+cname);
                return RespMessageUtil.getResp(RetCodeDef.SQL_ERROR, "");
            }
        } else {
            log.info("添加课程信息失败,参数鉴权失败");
            return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
        }

    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public RespMessage delete(String id) {
        if (StringUtil.isEmpty(id)) {
            log.info("删除课程失败,参数鉴权失败");
            return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
        }

        PageBean pageBean = getPageBean();
        TUserCouCriteria userCouCriteria = new TUserCouCriteria();
        TUserCouCriteria.Criteria criteria = userCouCriteria.createCriteria();
        if(!StringUtil.isEmpty(id)){
            criteria.andCidEqualTo(id);
            userCouService.selectPageBeanByExample(userCouCriteria,pageBean);
            List<TUserCou> list = pageBean.getResults();
            if(list.size()>0){
                log.info("添加删除信息失败,已经被人选择");
                return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
            }
        }
        boolean flag = courseService.deleteByPrimaryKey(id);
        if (flag) {
            log.info("添加课程基本信息成功,id="+id);
            return RespMessageUtil.getResp(RetCodeDef.SUCCESS, "");
        } else {
            log.info("添加课程信息失败,SQL异常,id="+id);
            return RespMessageUtil.getResp(RetCodeDef.SQL_ERROR, "");
        }


    }


}
